local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "agenthelper"
local msgproxy = require "msgproxy"
local processstate = require "processstate"
local configdao = require "configdao"
local table = table
require "enum"

local processing = processstate:new({timeout=4})
local  CreateFriendTable = {}

--[[
//请求创建朋友桌
message CreateFriendTableReq {
	optional Version version = 1;
	optional int32   gameid = 2;
	optional string  password = 3;  //密码 如果没有密码填空串
	optional int32   room_type = 4;   //创建房间模式, 2表示自建房卡场 3表示自建金币场
	optional int32   retain_to_num = 5;//创建房间局数
	optional int32   roomcardnum = 6;  //消耗房卡数
	optional string  gamecfg = 7;     //创建房间配置 json串，格式和没有游戏协议中的CreateTableCfg对应
}

//响应创建朋友桌
message CreateFriendTableRes {
	optional int32  errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional string create_table_id = 3; //朋友桌索引号
	optional int32  gameid = 4;  //游戏类型id 
}

]]

function  CreateFriendTable.process(session, source, fd, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid
	}
	local server = msghelper:get_server()

	--检查当前登陆状态
	if not msghelper:is_login_success() then
		filelog.sys_warning("CreateFriendTable.process invalid server state", server.state)
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		return
	end

	if processing:is_processing() then
		responsemsg.errcode = EErrCode.ERR_DEADING_LASTREQ
		responsemsg.errcodedes = "正在处理上一次请求！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		return
	end

	if request == nil or request.gameid == nil or request.gameid <= 0 then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		return
	end

	processing:set_process_state(true)
	local create_fndtable_num = msgproxy.sendrpc_reqmsgto_tablesvrd(server.rid, "get_fndtable_num", {rid=server.rid, gameid=request.gameid})
	if create_fndtable_num == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		return			
	end

	if create_fndtable_num >= configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "max_createtable_num") then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "您创建的房间数已满"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		return						
	end		

	--检查玩家是否有足够的宝石创建桌子
	local selfbuildtablecfgitem = msghelper:get_selfbuildtablecfgitem(request.gameid)
	if selfbuildtablecfgitem == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求，当前的游戏不存在！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		processing:set_process_state(false)
		return		
	end

	local money = server.money
	local roomcardnum = 0
	local is_find = false
	local is_enough_money = false
	for _, gamesselectionitem in ipairs(selfbuildtablecfgitem.gamesselections) do
		if gamesselectionitem.retain_to_num == request.retain_to_num then
			is_find = true
			if money.roomcard >= gamesselectionitem.roomcardnum then
				roomcardnum = gamesselectionitem.roomcardnum
				is_enough_money = true
			end
			break
		end
	end

	if not is_find then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效的请求，无效的请求参数！"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		processing:set_process_state(false)
		return		
	end

	if not is_enough_money then
		responsemsg.errcode = EErrCode.ERR_NOT_ENOUGH_GEMSTONE
		responsemsg.errcodedes = "房卡不足不能创建房间"
		msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)		
		processing:set_process_state(false)
		return				
	end

	request.rid = server.rid
	request.playerinfo = {
		rolename = server.info.rolename,
		logo = server.info.logo,
	}
	request.roomcardnum = roomcardnum		
	responsemsg = msgproxy.sendrpc_reqmsgto_roomsvrd(request.gameid, server.rid, nil, nil, "createfriendtable", request)
	processing:set_process_state(false)		

	if not msghelper:is_login_success() then
		return
	end
	
	if responsemsg ~= nil 
		and responsemsg.errcode == EErrCode.ERR_SUCCESS then
		msghelper:save_player_awards(server.rid, {{id=3, num=-roomcardnum}}, EReasonChangeCurrency.CHANGE_CURRENCY_CREATE_TABLE, responsemsg.create_table_id)
	end

	if responsemsg == nil then
		responsemsg = {
			errcode = EErrCode.ERR_INVALID_REQUEST,
			errcodedes = "无效的请求!",
		}
	end
	responsemsg.gameid = request.gameid
	msghelper:send_resmsgto_client(fd, "CreateFriendTableRes", responsemsg)
end

return CreateFriendTable

